﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataSet_fp_DyhTableAdapters;
using System.Net;

public partial class search : System.Web.UI.Page
{
    //public int seqID;
    protected void Page_Load(object sender, EventArgs e)
    {
        Literal_h_ads.Text = ADS.GetADsCode(6);
        Literal_v_ads.Text = ADS.GetADsCode(7);

        if (!IsPostBack)
        {
            int seqID;
            string keyword = Request.QueryString["keyword"];
            string type = Request.QueryString["sel"];

            //加入QueryString无效性的处理逻辑；


            //显示相应的信息类别
            MultiView1.ActiveViewIndex = Convert.ToByte(type) - 1;

            if (type == "1")
            {
                if (keyword.Substring(0, 1) == "0")
                {
                    DataList2.Enabled = true;
                    DataList2.Visible = true;
                    DataList1.Enabled = false;
                    DataList1.Visible = false;
                }
                else if(keyword.Substring(0,1)=="1")
                {
                    DataList1.Visible = true;
                    DataList1.Enabled = true;
                    DataList2.Enabled = false;
                    DataList2.Visible=false;
                }
                else
                {
                    Label_NoNumber.Enabled = true;
                    Label_NoNumber.Visible = true;
                    Label_NoNumber.Text = "无此电话号码信息！";
                }
            }
            else if (type == "2")
            {
                //网站信息处理逻辑
                Label_URL.Text = keyword.Trim();
                if (keyword.Trim().IndexOf("//", 0) > 0)
                {
                    HyperLink_URL.NavigateUrl = keyword;
                }
                else
                {
                    HyperLink_URL.NavigateUrl = "http://" + keyword.Trim();
                }
                HyperLink_URL.ToolTip = "点击访问：" + HyperLink_URL.NavigateUrl;


                //获取网址的IP地址或地址列表
                IPAddress[] IPCollect = Public.URL_to_All_IP(keyword.Trim());
                Label_URL_IP.Text = "";
                if (IPCollect != null)
                {
                    int i = 0;
                    foreach (IPAddress ips in IPCollect)
                    {
                        Label_URL_IP.Text = Label_URL_IP.Text + ips + (i == 0 ? " " : "");
                        i++;
                    }
                     //string ipaddress= IpSearch.GetAddressWithIP(IPCollect[0].ToString());
                     //string[] strs = ipaddress.Split(new string[] {"","","","" }, StringSplitOptions.None);
                    //if(ipaddress)
                    Label_URL_Address.Text = IpSearch.GetAddressWithIP(IPCollect[0].ToString());
                }
                else
                {
                    Label_URL_IP.Text = "不存在这样的主机IP";
                    Label_URL_Address.Text = "无此主机归属地信息！";
                }
            }
            else if (type == "3")
            {

                string ID_18="";
                //身份证处理逻辑
                if (keyword.Trim().Length == 18)
                {
                    Panel_18ID.Enabled = false;
                    Panel_18ID.Visible = false;

                    ID_18 = keyword;

                    Label_ID.Text = keyword.ToString().Trim();
                    Label_ID_Adress.Text = Public.GetID_Detail_Adress(keyword.Trim());
                    Label__ID_Birthday.Text = keyword.Substring(6, 4).Trim() + "年" + keyword.Substring(10, 2) + "月" + keyword.Substring(12, 2) + "日";
                    if (Convert.ToByte(keyword.Substring(16, 1).Trim()) % 2 == 1)
                    {
                        Label_ID_sex.Text = "男";
                    }
                    else
                    {
                        Label_ID_sex.Text = "女";
                    }

                    //判断ID是否有效
                    if (Label_ID_Adress.Text == "无此身份证地址信息！")
                    { 
                        Label_IDValid.Visible=true;
                        Label_IDValid.Text = "* 身份证地址编码（信息）有误，<br>&nbsp;&nbsp;请确认前6位的有效性！";
                    } 
                    else if (!Public.Date_Check(keyword.Substring(6,8)))  //判断日期是否有效。
                    {
                        Label_IDValid.Visible = true;
                        Label_IDValid.Text = "* 身份证日期编码有误，<br>&nbsp;&nbsp;请确认出生日期信息的有效性！";
                    }
                    else if (!Public.ID_CRC_IsValid(keyword))  //判断检验是否有效
                    {
                        Label_IDValid.Visible = true;
                        Label_IDValid.Text = "* 身份证校验有误，<br>&nbsp;&nbsp;请确认本身份证号的有效性！";
                    }
                }
                else if (keyword.Trim().Length == 15)
                {
                    Label_ID.Text = keyword.ToString().Trim();

                    //显示对应的18位ID
                    Panel_18ID.Enabled = true;
                    Panel_18ID.Visible = true;

                    Label_18ID.Text = keyword.Insert(6, "19") + Public.GetID_CRC_Data(keyword.Insert(6, "19"));// +IDCheck();

                    ID_18 = Label_18ID.Text;

                    Label_ID_Adress.Text = Public.GetID_Detail_Adress(keyword.Trim());
                    Label__ID_Birthday.Text = "19" + keyword.Substring(6, 2).Trim() + "年" + keyword.Substring(8, 2) + "月" + keyword.Substring(10, 2) + "日";
                    if (Convert.ToByte(keyword.Substring(14, 1).Trim()) % 2 == 1)
                    {
                        Label_ID_sex.Text = "男";
                    }
                    else
                    {
                        Label_ID_sex.Text = "女";
                    }

                    //判断ID是否有效
                    if (Label_ID_Adress.Text == "无此身份证地址信息！")
                    {
                        Label_IDValid.Visible = true;
                        Label_IDValid.Text = "* 身份证地址编码（信息）有误，<br>&nbsp;&nbsp;请确认前6位的有效性！";
                    }
                    else if (!Public.Date_Check(Label_18ID.Text.Substring(6, 8)))  //判断日期是否有效。
                    {
                        Label_IDValid.Visible = true;
                        Label_IDValid.Text = "* 身份证日期编码有误，<br>&nbsp;&nbsp;请确认出生日期信息的有效性！";
                    }
                }
                else
                {
                    Panel_show.Visible = false;
                    Label_IDValid.Visible = true;
                    Label_IDValid.Enabled = true;
                    Label_IDValid.Text = "*身份证号码位数无效，请重试！";
                }

                //if (Label_ID_Adress.Text != "无此身份证地址信息！" && Label_IDValid.Text != "* 身份证日期编码有误，<br>&nbsp;&nbsp;请确认出生日期信息的有效性！" && Label_IDValid.Text != "*身份证号码位数无效，请重试！")
                if (Label_IDValid.Visible == false)
                {
                    //HyperLink_ID_Check.Visible = true;
                    //HyperLink_ID_Check.NavigateUrl = "http://www.id5.cn/desktop/parse.do?type=3&icpcode=bhc04&imageField.x=36&imageField.y=9&checknum=" + ID_18;
                }
                else
                {
                    //HyperLink_ID_Check.Visible = false;
                }
            }
            else if (type == "4")
            { 
            //银行账号处理信息
                Label_Bank_ID.Text = keyword.Trim();
            }
            else if (type == "5")
            { 
            //QQ号码处理逻辑
                Label_QQ_ID.Text = keyword.Trim();
            }
            else if (type == "6")
            { 
            //邮箱处理逻辑
                Label_Email_ID.Text = keyword.Trim();
            }
            else if (type == "7")
            { 
            //网店处理逻辑
                Label_Bussness_ID.Text = keyword;
            }
            else if (type == "8")
            {
            //其它情况处理逻辑
                Label_Other_ID.Text = keyword.Trim();
            }



            //TextBox_copy.Text = GetUrl().Trim();// +"sel=" + type.ToString().Trim() + "keyword=" + keyword.Trim();

            Label_MPnumber.Text = keyword.ToString().Trim();
            //Label_MPnumber_1.Text = keyword.ToString().Trim();
            //Label_MPnumber_3.Text = keyword.ToString().Trim();
            //Label_mpnumber_4.Text = keyword.ToString().Trim();

            //HyperLink_search.Text = "去百度搜索 " + keyword.ToString().Trim() + " »";
            //HyperLink_search.NavigateUrl = "http://www.baidu.com/s?wd=" + keyword.ToString().Trim();

            //获取并显示信息类别名称
            Dyh_typeTableAdapter typeta = new Dyh_typeTableAdapter();
            Label_MPnumber.Text = typeta.Get_Data_By_Id(Convert.ToByte(type.Trim())).Rows[0]["info"].ToString() + Label_MPnumber.Text;
            //Label_type2.Text = typeta.Get_Data_By_Id(Convert.ToByte(type.Trim())).Rows[0]["info"].ToString();

            //Title = Label_type2.Text + "：" + keyword + " - 详细信息 - " + "IT110：不良信息举报与检索平台  - By ECountry.Ltd";

            //进行查询相关的存取及信息记录
            Dyh_baseDBTableAdapter baseta = new DataSet_fp_DyhTableAdapters.Dyh_baseDBTableAdapter(); 
            DataSet_fp_Dyh.Dyh_baseDBDataTable baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword.ToString().Trim(), Convert.ToByte(type));
            
            Dyh_searchTableAdapter searchta=new Dyh_searchTableAdapter();
            //DataSet_fp_Dyh.Dyh_searchDataTable searchDataTable=searchta.
            if ((int)baseDataTable.Rows.Count > 0)
            {
                baseta.Update_S_times_Query(keyword.ToString().Trim(), Convert.ToByte(type));
                baseta.Update_L_S_time_Query(keyword.ToString().Trim(), Convert.ToByte(type));
                seqID = (int)baseDataTable.Rows[0]["id"];

                HiddenField_seqID.Value = seqID.ToString().Trim();

                //Response.Write(seqID);
                searchta.Insert_New_Data_Query(seqID,Public.GetClientIP() ,Public.GetClientMAC(),Page.Server.MachineName.ToString(),Page.Request.UserHostName,"",IpSearch.GetAddressWithIP(Public.GetClientIP()),0,3,0);  //写入当前查询记录
            }  //Request.ServerVariables["REMOTE_ADDR"].ToString() .GetClientMAC().GetMACAddressString()
            else
            {
                baseta.Insert_New_Data_Query(keyword.ToString().Trim(), Convert.ToByte(type), 1, 0);
                baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword.ToString().Trim(), Convert.ToByte(type));

                seqID = (int)baseDataTable.Rows[0]["id"];
                HiddenField_seqID.Value = seqID.ToString().Trim();
                //Response.Write(seqID);
                //baseta.ins
                searchta.Insert_New_Data_Query(seqID, Public.GetClientIP(), Public.GetClientMAC(), Page.Server.MachineName.ToString(), Page.Request.UserHostName, "", "", 0, 3, 0);  //写入当前查询记录
            }
        }
        
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList radioButtonList_report=(RadioButtonList)FormView1.FindControl("RadioButtonList1");
        TextBox textBox_report_others=(TextBox)FormView1.FindControl("TextBox_Others");
        RequiredFieldValidator _requiredFieldVidator = (RequiredFieldValidator)FormView1.FindControl("RequiredFieldValidator3");
        if (radioButtonList_report.SelectedValue.Trim() == "其他")
        {
            textBox_report_others.Visible = true;
            _requiredFieldVidator.Enabled = true;
            //textBox_report_others.Enabled = true;
        }
        else
        {
            textBox_report_others.Visible = false;
            _requiredFieldVidator.Enabled = false;
            //textBox_report_others.Enabled = false;
        }
    }
    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        string keyword = Request.QueryString["keyword"];
        string type = Request.QueryString["sel"];


        RadioButtonList radioButtonList_report = (RadioButtonList)FormView1.FindControl("RadioButtonList1");
        TextBox textBox_report_others = (TextBox)FormView1.FindControl("TextBox_Others");

        if (radioButtonList_report.SelectedValue.Trim() == "其他")
        {
            e.Values["R_type"] = textBox_report_others.Text.Trim();
        }
        else if (radioButtonList_report.SelectedValue.Trim() == "不作选择，只发表评论")
        {
            e.Values["R_type"] = "只评论";
        }
        else
        {
            e.Values["R_type"] = radioButtonList_report.SelectedValue.Trim();
        }

        e.Values["seqID"] = Convert.ToInt32(HiddenField_seqID.Value);
        e.Values["IP"] = Public.GetClientIP();
        e.Values["MAC"] = Public.GetClientMAC();
        e.Values["R_typeID"] =Convert.ToByte(Request.QueryString["sel"]);

        //优化举报物理地址及用户名
        TextBox textBox_IP_from = (TextBox)FormView1.FindControl("IP_fromTextBox");
        if (textBox_IP_from.Text.Trim() == IpSearch.GetAddressWithIP(Public.GetClientIP()).Trim())
        {
        e.Values["IP_from"]=textBox_IP_from.Text.Trim()+"用户";
        //}
        //else
        //{
        //        e.Values["IP_from"]="";
        }

        //判断本信息是否为第一次举报，决定如何更新第一次举报时间及最后一次举报时间
        Dyh_baseDBTableAdapter baseta = new DataSet_fp_DyhTableAdapters.Dyh_baseDBTableAdapter();

        if ((int)baseta.Get_Data_By_keyword_and_type(keyword.ToString().Trim(), Convert.ToByte(type)).Rows[0]["R_times"] > 0)
        {

            //此处需加入判断本IP地址是否已经对本信息进行过举报，若举报过，则跳转到已经举报过的提示页面
            Dyh_reportTableAdapter reportta = new Dyh_reportTableAdapter();
            if ((int)reportta.Get_Data_By_seqID_and_IP(Public.GetClientIP(),Convert.ToInt32(HiddenField_seqID.Value)).Rows.Count > 0)  //.Rows[0]["id"]
            {
                string url = "doreport.aspx?" + "sel=" + type + "&keyword=" + keyword + "&done=1";
                Response.Redirect(url);
            }

            baseta.Update_L_R_time_Query(keyword.Trim(), Convert.ToByte(type.Trim()));
            baseta.Update_R_times_Query(keyword.Trim(), Convert.ToByte(type.Trim()));
        }
        else
        {
            baseta.Update_F_R_time_Query(keyword.Trim(), Convert.ToByte(type.Trim()));
            baseta.Update_L_R_time_Query(keyword.Trim(), Convert.ToByte(type.Trim()));
            baseta.Update_R_times_Query(keyword.Trim(), Convert.ToByte(type.Trim()));
        }

        //DataSet_fp_Dyh.Dyh_baseDBDataTable baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword.ToString().Trim(), Convert.ToByte(type));


    }
    protected void RadioButtonList1_DataBound(object sender, EventArgs e)
    {
        RadioButtonList radioButtonList_report = (RadioButtonList)FormView1.FindControl("RadioButtonList1");
        radioButtonList_report.Items.FindByValue("不作选择，只发表评论").Selected=true;
    }
    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        TextBox textBox_IP_from = (TextBox)FormView1.FindControl("IP_fromTextBox");
        textBox_IP_from.Text = IpSearch.GetAddressWithIP(Public.GetClientIP());
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        //此处要加入成功添加报告记录后的举报次数的更新，及页面跳转。

        string url = "doreport.aspx?sel=" +Request.QueryString["sel"].ToString().Trim() + "&keyword=" + Request.QueryString["keyword"].ToString().Trim() + "&done=0";
        Response.Redirect(url);

    }

    /// <summary>
    /// 获取当前页URL地址
    /// </summary>
    private string GetUrl()
    {

        //取本页URL地址 
        string strTemp = "";
        if (Request.ServerVariables["HTTPS"] == "off")
        {
            strTemp = "http://";
        }
        else
        {
            strTemp = "https://";
        }

        strTemp = strTemp + Request.ServerVariables["SERVER_NAME"];

        if (Request.ServerVariables["SERVER_PORT"] != "80")
        {
            strTemp = strTemp + ":" + Request.ServerVariables["SERVER_PORT"];
        }

        strTemp = strTemp + Request.ServerVariables["URL"];

        if (Request.QueryString != null)
        {
            strTemp = strTemp + "?" + Request.QueryString;
        }

        return strTemp;
    }


    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        string keyword = Request.QueryString["keyword"];
        string type = Request.QueryString["sel"];
        //Label_count.Text = ListView1.Items.Count.ToString();  // ObjectDataSource_Report_View.SelectParameters.Count.ToString();

        if (ListView1.Items.Count > 0)
        {
            Label_MPnumber_2.Text = "";
        }
        else
        {
            Label_MPnumber_2.Text = "目前没有任何用户报告或评论。";
        }
    }
}
